package cn.ming.leo.service;

import java.util.Date;

import org.springframework.stereotype.Service;

import cn.ming.leo.bean.Door;
import cn.ming.leo.bean.DoorTools;
import cn.ming.leo.bean.Tools;
import cn.ming.leo.utils.Constant.MsgCode;
import cn.ming.leo.utils.Message;
import cn.ming.leo.utils.PageListModel;
import cn.ming.leo.utils.TimeFormat;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.PagedList;
import com.avaje.ebean.SqlRow;

@Service
public class ToolsService {

	public Message<String> insert(Integer id, Integer roles, String logoUrl, String name, String spec, int borrowDay,
			int doorId) {

		if (id == 0) {
			Door door = Door.find.byId("" + doorId);

			if (door.getDoorStatus() == 1) {
				return new Message<String>(MsgCode.FAIL.value, "已经有工具存储，请重新确认");
			}
			Tools tools = new Tools();
			tools.setLogoUrl(logoUrl);
			tools.setName(name);
			tools.setSpec(spec);
			tools.setRoles(roles);
			tools.setBorrowDay(borrowDay);
			tools.setCreateTime(TimeFormat.nowTime());
			tools.setUpdateTime(new Date());
			tools.insert();
			tools.refresh();

			door.setToolsId(tools.getId());
			door.setDoorStatus(1);
			door.setUpdateTime(new Date());
			door.update();
		} else {
			Tools tools = Tools.find.byId("" + id);
			tools.setLogoUrl(logoUrl);
			tools.setName(name);
			tools.setSpec(spec);
			tools.setRoles(roles);
			tools.setBorrowDay(borrowDay);
			tools.setUpdateTime(new Date());
			tools.update();
			Door door = Door.find.byId("" + doorId);

			door.setToolsId(tools.getId());
			door.setDoorStatus(1);
			door.setUpdateTime(new Date());
			door.update();
		}

		return new Message<String>("success");
	}

	public Message<String> updateStatus(int id, int status) {
		Tools box = Tools.find.byId("" + id);
		box.setToolsStatus(status);
		box.update();

		String sql = "select id from door_tools where tools_id = " + id + " order by create_time desc limit 1";
		SqlRow sqlRow = Ebean.createSqlQuery(sql).findUnique();
		DoorTools doorTools = DoorTools.find.byId(sqlRow.getString("id"));
		doorTools.setStatus(status);
		doorTools.update();

		return new Message<String>("success");
	}

	public Message<PageListModel<Tools>> findAll(Integer userId, Integer pageIndex, Integer pageSize) {
		PagedList<Tools> pl;
		ExpressionList<Tools> pression = Ebean.find(Tools.class).where();

		pageIndex = (pageIndex == null) ? 0 : pageIndex - 1;
		pageSize = (pageSize == null) ? 10 : pageSize;

		pl = pression.order().desc("create_time").findPagedList(pageIndex, pageSize);

		PageListModel<Tools> pageListModel = new PageListModel<Tools>(pl);

		for (Tools tools : pageListModel.getList()) {
			Door door = Door.find.where().eq("tools_id", tools.getId()).findUnique();
			tools.setDoorName(door.getBox().getBoxNo() + "柜子编号" + door.getDoorNo() + "号格子");

			if (tools.getToolsStatus() == 1) {

				String sql1 = "select id from door_tools where tools_id = " + tools.getId()
						+ " order by create_time desc limit 1";
				SqlRow row = Ebean.createSqlQuery(sql1).findUnique();

				DoorTools doorTools = DoorTools.find.byId(row.getString("id"));
				tools.setTimeStr(TimeFormat.hourDiff(doorTools.getCreateTime()));

				tools.setUserId(doorTools.getPutUserId());
				if(doorTools.getPutUserId() == userId) {
					tools.setFlag(true);
				}
			}
		}
		return new Message<PageListModel<Tools>>(pageListModel);
	}
}
